import requests
from threading import Thread
import time
from multiprocessing import Process

# 利用Python实现一个多线程程序

def printNum(num):
    for i in range(num):
        time.sleep(0.2)
        print(f"当前数字为：{i}")


def goThread():
    old = time.time()
    thread_array = []
    time.sleep(0.1)
    t = Thread(target=printNum, args=(5,))
    thread_array.append(t)
    t.start()
    for t in thread_array:
        t.join()
    now = time.time()

    print(f"线程done!!!, 用时{round((now - old),2)} s ！")

# 将多线程程序改为多进程程序

def goProcess():
    old = time.time()
    process_array = []
    time.sleep(0.1)
    p = Process(target=printNum,args=(7,))
    process_array.append(p)
    p.start()
    for p in process_array:
        p.join()
    now = time.time()

    print(f"进程done!!!, 用时{round((now - old),2)} s ！")

if __name__ == "__main__":
    goThread()
    print("进程")
    goProcess()













'''
def request_baidu():
    time.sleep(2)
    url = "https://www.baidu.com/"
    body = " "
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }
    response = requests.get(url = url, headers = headers)
    # print(response.text)
    print(response)
'''